package edu.langnan.xiaoli.controller.business;

import edu.langnan.xiaoli.base.AjaxResult;
import edu.langnan.xiaoli.base.controller.BaseController;
import edu.langnan.xiaoli.config.security.LoginUser;
import edu.langnan.xiaoli.config.security.TokenService;
import edu.langnan.xiaoli.constant.HttpStatus;
import edu.langnan.xiaoli.core.dto.LoginBodyDTO;
import edu.langnan.xiaoli.core.dto.LoginInfoDTO;
import edu.langnan.xiaoli.core.dto.StudentDTO;
import edu.langnan.xiaoli.service.StudentService;
import jdk.nashorn.internal.parser.Token;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author cql
 */
@RestController
@RequestMapping("auth")
public class LoginController extends BaseController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private TokenService tokenService;

    /**
     * 用户登录
     *
     * @param loginBody
     * @return
     */
    @PostMapping("login")
    public AjaxResult login(@RequestBody LoginBodyDTO loginBody) {
        LoginInfoDTO token = studentService.login(loginBody);
        return AjaxResult.success(HttpStatus.OK.getMessage(), token);
    }

    /**
     * 获取学生基本信息
     *
     * @return
     */
    @GetMapping("info")
    public AjaxResult stuInfo(HttpServletRequest request) {
        LoginUser loginUser = tokenService.getLoginUser(request);
        StudentDTO studentDTO = studentService.findStuInfo(loginUser);
        return AjaxResult.success(studentDTO);
    }


}
